package com.zretc.point.controller;

import com.zretc.api.pojo.Commodity;
import com.zretc.common.constant.WebConstant;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.point.service.CommodityService;
import com.zretc.security.entity.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/store")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    /*
     * 获取单件商品详情
     */
    @PostMapping("/commodityDetail")
    public RespResult getCommodityDetail(@RequestBody Commodity commodity){
        System.out.println(commodity.getComId()); // test
        Commodity comInfo = commodityService.getCommodityById(commodity.getComId());
        if(Objects.isNull(comInfo))
        {
            return R.fail(404, WebConstant.ERROR_404);
        }
        else
        {
            return R.success(comInfo);
        }
    }

    /*
     * 分页查询全部商品
     */
    @GetMapping("/commodityAll")
    public RespResult getCommodityList(@RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pageSize") Integer pageSize){
        return R.success(commodityService.queryCommodity(pageNum, pageSize));
    }

    /*
     * 分页查询在售商品
     */
    @GetMapping("/commodityForUser")
    public RespResult getCommodityOnSaleList(@RequestParam("target") String target, @RequestParam("pageNum") Integer pageNum,
                                             @RequestParam("pageSize") Integer pageSize){
        return R.success(commodityService.queryCommodityOnSale(pageNum, pageSize, target));
    }

    /*
     * 新增商品
     */
    @PostMapping ("/addCommodity")
    public RespResult addCommodity(@RequestBody Commodity commodity){
        return R.success(commodityService.addCommodity(commodity));
    }

    /*
     * 逻辑删除
     */
    @PutMapping ("/deleteCommodity")
    public RespResult deleteCommodity(@RequestBody Commodity commodity){
        System.out.println("CommodityController-------------------------------------"); // test
        System.out.println(commodity); // test
        return R.success(commodityService.deleteCommodity(commodity.getComId()));
    }

    /*
     * 逻辑恢复
     */
    @PutMapping  ("/restoreCommodity")
    public RespResult restoreCommodity(@RequestBody Commodity commodity){
        System.out.println("CommodityController--------------------------------------"); //test
        System.out.println(commodity); // test
        return R.success(commodityService.restoreCommodity(commodity.getComId()));
    }

    /*
     * 购买商品
     */
    @PostMapping("/buy")
    public RespResult buy(@RequestBody Commodity commodity){
        System.out.println("CommodityController==============================================="); //test
        System.out.println(commodity);

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
        System.out.println(user.getUser()); // test
        return commodityService.buyCommodity(commodity.getComId(), user.getUser().getUserId());
    }
}
